import { createRouter, createWebHistory } from 'vue-router'

const pages = import.meta.glob('../views/**/page.js', { eager: true, import: 'default' })
const components = import.meta.glob('../views/**/index.vue')

const routes: Array<any> = Object.entries(pages).map(([path, meta]) => {
  const compPath = path.replace('page.js', 'index.vue')
  path = path.replace('../views', '').replace('page.js', '')
  const name = path.split('/').filter(Boolean).join('-') || 'index'
  return {
    path,
    name,
    component: components[compPath],
    meta
  }
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

export default router